package sicnu.cs.ich.ich.service.impl;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.stereotype.Service;
import sicnu.cs.ich.api.domain.dto.IchProvinceDTO;

import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author 沈洋 邮箱:1845973183@qq.com
 * @create 2021/11/24-19:27
 **/
@Service
public class GuavaCacheService {
    Cache<Integer, IchProvinceDTO> provinceDTOGuavaCache;

    @PostConstruct
    public void init() {
        provinceDTOGuavaCache = CacheBuilder.newBuilder()
                .initialCapacity(50)
                .concurrencyLevel(5)
                .expireAfterAccess(10, TimeUnit.MINUTES)
                .build();
    }

    public IchProvinceDTO getIchProvinceDTO(Integer province) throws ExecutionException {
        return provinceDTOGuavaCache.getIfPresent(province);
    }

    public void setIchProvinceDTO(Integer province, IchProvinceDTO ichProvinceDTO) throws ExecutionException {
        provinceDTOGuavaCache.put(province, ichProvinceDTO);
    }
}
